package com.sgb.kjwl.view.ui.fragment.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrSearchNewChildBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.kjwl.view.ui.activity.pick.TenderDetailsActivity3;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration2;
import com.swgk.core.view.selection.CommonPopEntity;
import com.sgb.kjwl.model.entity.LeaseSearchResultEntity;
import com.sgb.kjwl.model.entity.LeaseSearchResultTypeEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.home.SearchActivity5;
import com.sgb.kjwl.view.ui.activity.pick.PickNoticeBidDetailActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickNoticeDetailActivity;
import com.sgb.jicai.view.adapter.CentralizedMiningCuttingLineAdapter;
import com.sgb.jicai.view.adapter.FactoryCentralizedListItemAdapter2;
import com.sgb.kjwl.view.ui.adapter.home.LeaseSearchResultAdapter;
import com.sgb.kjwl.view.ui.adapter.machine.LeaseSearchResultAskAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickBidingAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickHallBidingAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickWinningNoticeAdapter;
import com.sgb.kjwl.viewmodel.home.SearchNewChildFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class SearchNewChildFragment extends AppLazyFragment {

    @Inject
    SearchNewChildFragModel viewModel;
    private FrSearchNewChildBinding binding;

    private String mSearchType;
    private String mKeyWord;
    private int currentIndex = 0;
    private int currentPage = 1;
    private String currentId = "";

    private PickHallBidingAdapter pickingAdapter;//招标适配器
    private PickBidingAdapter pickingBigAdapter;//竞价适配器
    private PickWinningNoticeAdapter pickWinningNoticeAdapter;//中标公告
    private LeaseSearchResultAdapter adapter;//出租适配器
    private LeaseSearchResultAskAdapter askAdapter;//求租适配器
    private CentralizedMiningCuttingLineAdapter centralizedMiningAdapter;//需方适配器
    private FactoryCentralizedListItemAdapter2 factoryCentralizedMiningAdapter;//供方适配器

    public static SearchNewChildFragment newInstance(String code, String keyWord, int index) {
        SearchNewChildFragment fragment = new SearchNewChildFragment();
        Bundle bundle = new Bundle();
        bundle.putString("TYPE_SEARCH", code);
        bundle.putString("KEY_WORD", keyWord);
        bundle.putInt("INDEX", index);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_search_new_child, container, false);
        return binding.getRoot();
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    private void initFilterData(int page, boolean isLoadMore) {

        if (TextUtils.equals("recruitment", mSearchType) && !TextUtils.isEmpty(currentId)) {
            switch (currentIndex) {
                case 0:
                    viewModel.getPickTenderList(currentId, mKeyWord, page, isLoadMore);
                    break;
                case 1:
                    viewModel.getPickBigList(currentId, mKeyWord, page, isLoadMore);
                    break;
                case 2:
                    viewModel.getPickBigNoticeList(currentId, mKeyWord, page, isLoadMore);
                    break;
            }
            return;
        }
        if (TextUtils.equals("collect", mSearchType) && !TextUtils.isEmpty(currentId)) {
            switch (currentIndex) {
                case 0:
                    viewModel.getJiCaiList(mKeyWord, page, isLoadMore, currentId);
                    break;
                case 1:
                    viewModel.getFactoryCentralizedList(mKeyWord, page, isLoadMore, currentId);
                    break;
            }
            return;
        }
        if (TextUtils.equals("rent", mSearchType) && !TextUtils.isEmpty(currentId)) {
            switch (currentIndex) {
                case 0:
                    viewModel.getLeaseHomeSearch("1", mKeyWord, currentId, page, isLoadMore);
                    break;
                case 1:
                    viewModel.getLeaseHomeSearch("2", mKeyWord, currentId, page, isLoadMore);
                    break;
            }
            return;
        }

        switch (mSearchType) {
            case "recruitment":
                if (currentIndex == 1) {
                    viewModel.getFilterTypeData();
                } else {
                    viewModel.getPickFilterType();
                }
                break;
            case "collect":
                centralizedMiningAdapter = null;
                factoryCentralizedMiningAdapter = null;
                if (currentIndex == 0) {
                    viewModel.groupBuyClassList();
                }
                if (currentIndex == 1) {
                    viewModel.getTenderGoodsCategoryAllList("0");
                }
                break;
            case "rent":
                viewModel.getLeaseType();
                break;
        }
    }

    @Override
    protected void initView() {
        if (getArguments() != null) {
            mSearchType = getArguments().getString("TYPE_SEARCH");
            currentIndex = getArguments().getInt("INDEX");
        }
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.setPullRefreshEnabled(false);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                currentPage = currentPage + 1;
                initFilterData(currentPage, true);
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        if (getActivity() instanceof SearchActivity5){
            mKeyWord = ((SearchActivity5) getActivity()).getKeyWord();
        }
        currentId = "";
        initFilterData(currentPage, false);
    }

    @Override
    public void onVisible() {
        if (getActivity() instanceof SearchActivity5){
            String keyWord = ((SearchActivity5) getActivity()).getKeyWord();
            if (!TextUtils.equals(mKeyWord, keyWord)) {
                mKeyWord = keyWord;
                initFilterData(currentPage, false);
            }
        }
    }

    public void setPickFilterType(List<PickSelectEntity> data) {
        if (data == null) {
            return;
        }
        binding.rvChildTabLayout.setVisibility(View.VISIBLE);
        binding.rvChildTabLayout.removeAllTabs();
        binding.rvChildTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.rvChildTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                currentPage = 1;
                if (tab.getPosition() == 0) {
                    currentId = "0";
                } else {
                    currentId = data.get(tab.getPosition()).getNo();
                }
                initFilterData(currentPage, false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        for (int i = 0; i < data.size(); i++) {
            binding.rvChildTabLayout.addTab(binding.rvChildTabLayout.newTab().setText(data.get(i).getProjectscale()));
        }
    }

    public void showPickTenderList(List<TenderListEntity> list, boolean isLoadMore, boolean isLast) {
        if (pickingAdapter == null) {
            pickingAdapter = new PickHallBidingAdapter(getActivity(), null);
            binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
            binding.recyclerView.removeAllViews();
            binding.recyclerView.setAdapter(pickingAdapter);
        }
        pickingAdapter.setOnItemClick(new AdapterOnItemClick<TenderListEntity>() {
            @Override
            public void onItemClick(TenderListEntity tenderListEntity, int position) {
                if (tenderListEntity.getStatus().equals("8")) {   //待报名
                    PickNoticeDetailActivity.start(getActivity(), tenderListEntity.getId() + "");
                } else {
                    TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId() + "", 0);
                }
            }
        });
        pickingAdapter.onIChildClick(new PickHallBidingAdapter.IChildClick() {
            @Override
            public void onClick(TenderListEntity tenderListEntity) {
                if (tenderListEntity.getStatus().equals("8")) {   //待报名
                    PickNoticeDetailActivity.start(getActivity(), tenderListEntity.getId() + "");
                } else {
                    TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId() + "", 0);
                }
            }
        });
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                pickingAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                pickingAdapter.setDatas(list);
            } else {
                pickingAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void showPickBigList(List<BiddingEntity> list, boolean isLoadMore, boolean isLast) {
        if (pickingBigAdapter == null) {
            pickingBigAdapter = new PickBidingAdapter(getActivity(), null);
            binding.recyclerView.removeAllViews();
            binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
            binding.recyclerView.setAdapter(pickingBigAdapter);
        }

        pickingBigAdapter.setOnItemClick(new AdapterOnItemClick<BiddingEntity>() {
            @Override
            public void onItemClick(BiddingEntity tenderListEntity, int position) {
                viewModel.getUrlSetting(tenderListEntity.getId());
            }
        });
        pickingBigAdapter.onIChildClick(new PickBidingAdapter.IChildClick() {
            @Override
            public void onClick(BiddingEntity tenderListEntity) {
                viewModel.getUrlSetting(tenderListEntity.getId());
            }
        });

        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                pickingBigAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                pickingBigAdapter.setDatas(list);
            } else {
                pickingBigAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void showPickBigNoticeList(List<PreNoticeItemEntity> list, boolean isLoadMore, boolean isLast) {
        if (pickWinningNoticeAdapter == null) {
            pickWinningNoticeAdapter = new PickWinningNoticeAdapter(getActivity(), null);
            binding.recyclerView.removeAllViews();
            binding.recyclerView.setAdapter(pickWinningNoticeAdapter);
        }
        pickWinningNoticeAdapter.setOnItemClick(new AdapterOnItemClick<PreNoticeItemEntity>() {
            @Override
            public void onItemClick(PreNoticeItemEntity preNoticeItemEntity, int position) {
                //中标
                PickNoticeBidDetailActivity.start(getActivity(), preNoticeItemEntity.getTenderNo() + "");
            }
        });
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                pickWinningNoticeAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                pickWinningNoticeAdapter.setDatas(list);
            } else {
                pickWinningNoticeAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setCollectFilter1Type(List<PickSelectEntity> data) {
        if (data == null) {
            return;
        }
        binding.rvChildTabLayout.setVisibility(View.VISIBLE);
        binding.rvChildTabLayout.removeAllTabs();
        binding.rvChildTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.rvChildTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                currentPage = 1;
                currentId = data.get(tab.getPosition()).getNo();
                initFilterData(currentPage, false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        data.add(0, new PickSelectEntity("0", "全部"));
        for (int i = 0; i < data.size(); i++) {
            binding.rvChildTabLayout.addTab(binding.rvChildTabLayout.newTab().setText(data.get(i).getProjectscale()));
        }
    }

    public void setCollectFilter2Type(List<CommonPopEntity> data) {
        if (data == null) {
            return;
        }
        binding.rvChildTabLayout.setVisibility(View.VISIBLE);
        binding.rvChildTabLayout.removeAllTabs();
        binding.rvChildTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.rvChildTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                currentPage = 1;
                currentId = data.get(tab.getPosition()).getNo();
                initFilterData(currentPage, false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        data.add(0, new CommonPopEntity("0", "全部"));
        for (int i = 0; i < data.size(); i++) {
            binding.rvChildTabLayout.addTab(binding.rvChildTabLayout.newTab().setText(data.get(i).getTypeName()));
        }
    }

    public void setRentFilterType(List<LeaseSearchResultTypeEntity> data) {
        if (data == null) {
            return;
        }
        binding.rvChildTabLayout.setVisibility(View.VISIBLE);
        binding.rvChildTabLayout.removeAllTabs();
        binding.rvChildTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.rvChildTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                currentPage = 1;
                if (tab.getPosition() == 0) {
                    currentId = "0";
                } else {
                    currentId = data.get(currentIndex).getCategory().get(tab.getPosition()).getType();
                }
                initFilterData(currentPage, false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        for (int i = 0; i < data.get(currentIndex).getCategory().size(); i++) {
            binding.rvChildTabLayout.addTab(binding.rvChildTabLayout.newTab().setText(data.get(currentIndex).getCategory().get(i).getName()));
        }

    }

    public void showRentList(List<LeaseSearchResultEntity> list, boolean isLoadMore, boolean isLast) {

        if (currentIndex == 0) {
            if (adapter == null) {
                binding.recyclerView.addItemDecoration(new RecyclerViewItemDecoration2(getResources().getDimension(R.dimen.dp10), getResources().getDimension(R.dimen.dp10)));
                binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
                adapter = new LeaseSearchResultAdapter(getActivity(), null);
                binding.recyclerView.removeAllViews();
                binding.recyclerView.setAdapter(adapter);
            }
            adapter.setOnItemClick(new AdapterOnItemClick<LeaseSearchResultEntity>() {
                @Override
                public void onItemClick(LeaseSearchResultEntity entity, int position) {
                    if (!ClickUtils.isDoubleClick()) {
                        viewModel.getH5Url(entity.getDataId(), entity.getType());
                    }
                }
            });
            if (isLoadMore) {
                binding.recyclerView.loadMoreComplete();
                if (list != null && list.size() > 0) {
                    adapter.addDatas(list);
                }
                binding.recyclerView.setNoMore(isLast);
            } else {
                binding.recyclerView.refreshComplete();
                if (list != null && list.size() > 0) {
                    binding.emptyView.setVisibility(View.GONE);
                    adapter.setDatas(list);
                } else {
                    adapter.clearDatas();
                    binding.emptyView.setVisibility(View.VISIBLE);
                }
            }
        } else if (currentIndex == 1) {
            if (askAdapter == null) {
                binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
                askAdapter = new LeaseSearchResultAskAdapter(getActivity(), null);
                binding.recyclerView.removeAllViews();
                binding.recyclerView.setAdapter(askAdapter);
            }

            askAdapter.setOnItemClick(new AdapterOnItemClick<LeaseSearchResultEntity>() {
                @Override
                public void onItemClick(LeaseSearchResultEntity entity, int position) {
                    if (!ClickUtils.isDoubleClick()) {
                        viewModel.getAskH5Url(entity.getDataId(), entity.getType());
                    }
                }
            });
            if (isLoadMore) {
                binding.recyclerView.loadMoreComplete();
                if (list != null && list.size() > 0) {
                    askAdapter.addDatas(list);
                }
                binding.recyclerView.setNoMore(isLast);
            } else {
                binding.recyclerView.refreshComplete();
                if (list != null && list.size() > 0) {
                    binding.emptyView.setVisibility(View.GONE);
                    askAdapter.setDatas(list);
                } else {
                    askAdapter.clearDatas();
                    binding.emptyView.setVisibility(View.VISIBLE);
                }
            }
        }

    }

    public void showCollectFilter1List(List<CentralizedMiningEntity> list, boolean isLoadMore, boolean isLast) {
        if (centralizedMiningAdapter == null) {
            centralizedMiningAdapter = new CentralizedMiningCuttingLineAdapter(getActivity(), null);
            binding.recyclerView.removeAllViews();
            binding.recyclerView.setAdapter(centralizedMiningAdapter);
        }
        centralizedMiningAdapter.setOnItemClick(new AdapterOnItemClick<CentralizedMiningEntity>() {
            @Override
            public void onItemClick(CentralizedMiningEntity centralizedMiningEntity, int position) {
                viewModel.getH5UrlCollect(centralizedMiningEntity.getNo(), "collection");
            }
        });
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                centralizedMiningAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                centralizedMiningAdapter.setDatas(list);
            } else {
                centralizedMiningAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }


    public void showCollectFilter2List(List<FactoryCentralizedItemEntity> list, boolean isLoadMore, boolean isLast) {
        if (factoryCentralizedMiningAdapter == null) {
            factoryCentralizedMiningAdapter = new FactoryCentralizedListItemAdapter2(getActivity(), null, "1");
            binding.recyclerView.removeAllViews();
            binding.recyclerView.setAdapter(factoryCentralizedMiningAdapter);
        }

        factoryCentralizedMiningAdapter.setOnItemClick(new AdapterOnItemClick<FactoryCentralizedItemEntity>() {
            @Override
            public void onItemClick(FactoryCentralizedItemEntity factoryCentralizedItemEntity, int position) {
                viewModel.getH5UrlCollect(factoryCentralizedItemEntity.getNo(), "Factory");
            }
        });


        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                factoryCentralizedMiningAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                factoryCentralizedMiningAdapter.setDatas(list);
            } else {
                factoryCentralizedMiningAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void refreshChildView(String mSearchType, String mKeyWord) {
        this.mSearchType = mSearchType;
        if(currentId.equals("0")){
            currentId = "";
        }
        this.mKeyWord = mKeyWord;
        initFilterData(currentPage, false);
    }

    public void getUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }

    public void getUrl(String url, String id) {
        WebViewX5HostActivity.start(getActivity(), url, id);
    }
}
